<template>
  <div class="table">
    <div class="header">
      <slot name="header">
        <div class="title">{{ title }}</div>
        <div class="handler">
          <slot name="headerHandler"></slot>
        </div>
      </slot>
    </div>
    <el-table :data="List" border style="width: 100%" highlight-current-row>
      <el-table-column
        type="selection"
        width="55"
        v-if="isShowSelectColunm"
        flex="'left'"
      />
      <el-table-column
        v-if="isShowIndexColunm"
        align="center"
        type="index"
        label="序号"
        width="80"
      ></el-table-column>
      <template v-for="item in propList" :key="item.prop">
        <el-table-column
          v-bind="item"
          align="center"
          :flex="item.flex ? 'right' : false"
          show-overflow-tooltip
        >
          <template #default="scope">
            <slot :name="item.slotName" :row="scope.row">
              {{ scope.row[item.prop] }}
            </slot>
          </template>
        </el-table-column>
      </template>
      <el-table-column fixed="right" label="操作" width="150">
        <template #default="scope">
          <slot name="handle" :row="scope.row"></slot>
        </template>
      </el-table-column>
    </el-table>
    <div class="footer">
      <slot name="footer">
        <el-pagination
          background
          :page-size="page.pageSize"
          :current-page="currentPage"
          layout="prev, pager, next"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </slot>
    </div>
  </div>
</template>

<script>
import { useStore } from "vuex";
import { reactive, ref, computed } from "vue";
export default {
  props: {
    title: {
      type: String,
      default: "",
    },
    isShowIndexColunm: {
      type: Boolean,
      default: true,
    },
    page: {
      type: Object,
      default: () => {
        return {
          currentPage: 1,
          pageSize: 10,
        };
      },
    },

    isShowSelectColunm: {
      type: Boolean,
      default: false,
    },
    propList: {
      type: Array,
      default: () => [],
    },
    List: {
      type: Array,
      default: () => [],
    },
    storeName: {
      type: String,
      default: "",
    },
    name: {
      type: String,
      default: "",
    },
  },
  setup(props, { emit }) {
    const store = useStore();
    const isClickPagenation = ref(false);
    const handleCurrentChange = (currentPage) => {
      isClickPagenation.value = true;
      emit("update:page", { ...props.page, currentPage });
    };
    const handleSizeChange = (pageSize) => {
      emit("update:page", { ...props, pageSize });
    };
    const total = computed(
      () => store.state[`${props.storeName}`][`${props.name}Count`]
    );
    const currentPage = computed(() => {
      let currenge = props?.page.currentPage;
      if (isClickPagenation.value) {
        return currenge;
      } else {
        return currenge + 1;
      }
    });
    return {
      handleCurrentChange,
      handleSizeChange,
      total,
      currentPage,
    };
  },
};
</script>

<style lang="scss" scope>
.table {
  overflow: hidden;
  padding-bottom: 50px;
}
.header {
  display: flex;
  height: 45px;
  padding: 0 5px;
  justify-content: space-between;
  align-items: center;

  .title {
    font-size: 20px;
    font-weight: 700;
  }

  .handler {
    align-items: center;
  }
}
.footer {
  margin-top: 15px;

  .el-pagination {
    text-align: right;
    display: flex;
    justify-content: flex-end;
    padding-right: 40px;
  }
}
</style>
